package Model;

import java.util.ArrayList;

import Core.Board;
import Core.Game;
import Core.Player;
import Core.Position;
import Core.Symbol;

public class Morpion extends Game
{
    public Morpion(Player player1, Player player2, Board board)
    {
	super(player1, player2, board);
    }
    
    private int valueForSymbol(Symbol s) {
        if (s.equals(MorpionSymbol.CROSS)) {
            return +1;
        } else if (s.equals(MorpionSymbol.CIRCLE)) {
            return -1;
        }
        return 0;
    }
    
    public void play()
    {
        this.currentPlayer = player2;
        this.board.display();
        while (!gameIsFinished()) {
         
           Position position =  this.currentPlayer.play(this.validMoves(board),this.board);
         
           if (board.isValid(position)
           	&& board.isFree(position)) 	
           {
               System.out.println("bonjour");
            board.set(position, currentPlayer.getSymbole());
            this.board.display();
            this.setChanged();
	    this.notifyObservers(position);
            if (this.currentPlayer == this.player1) {
        	this.currentPlayer = this.player2;
            } else {
        	this.currentPlayer = this.player1;
            }
           }
        }

    }
     
    @Override
    public boolean gameIsFinished()
    {
	if (this.alignmentFound(this.alignement) || this.gridIsFilled())
	    return true;
	return false;
    }

    @Override
    public boolean gameIsDraw()
    {
	if (this.gridIsFilled())
	    return true;
	 return false;
    }

    @Override
    public ArrayList<Position> validMoves(Board board)
    {
	ArrayList<Position> validMoves = new ArrayList<Position>();
	for (int i=0; i<this.board.getLines(); i++) {
            for (int j=0; j<this.board.getColumns(); j++) {
        	Position position = new Position(i,j);
                if (this.board.getSymbol(position).equals(MorpionSymbol.NONE))
                    validMoves.add(position);
            }
        }
	return validMoves;
    }

}
